﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.IO.IsolatedStorage;
using System.Xml.Linq;

namespace Vision.VisionWorkshop.Common
{
    public static class ApplicationSettings
    {
        #region Declarations
        private static Point? fow_location = null;
        private static Size? fow_size = null;
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the location of the FileOpen Window dialog
        /// </summary>
        public static Point? FileOpen_Window_Location
        {
            get
            {
                return fow_location;
            }
            set
            {
                if (fow_location != value)
                {
                    fow_location = value;

                    updateSettings();
                }
            }
        }

        /// <summary>
        /// Gets or sets the size of the FileOpen Window dialog
        /// </summary>
        public static Size? FileOpen_Window_Size
        {
            get
            {
                return fow_size;
            }
            set
            {
                if (fow_size != value)
                {
                    fow_size = value;

                    updateSettings();
                }
            }
        }
        #endregion

        #region Constructor
        static ApplicationSettings()
        {
            loadSettings();
        }
        #endregion

        #region load/update settings
        private static void loadSettings()
        {
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var isoStream = storage.OpenFile("ApplicationSettings.xml", System.IO.FileMode.OpenOrCreate))
                {
                    try
                    {
                        parseSettingsDocument(XDocument.Load(isoStream));
                    }
                    catch
                    {
                    }
                }
            }
        }

        private static void updateSettings()
        {
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var isoStream = storage.OpenFile("ApplicationSettings.xml", System.IO.FileMode.Create))
                {
                    try
                    {
                        XDocument xSettings = buildSettingsDocument();

                        xSettings.Save(isoStream);
                    }
                    catch
                    {
                    }
                }
            }
        }
        #endregion

        #region read/build settings documents
        private static void parseSettingsDocument(XDocument xDocument)
        {
            XElement fowLocation = xDocument.Root.Element("FileOpenWindow").Element("Location");
            if (fowLocation != null)
                fow_location = new Point(double.Parse(fowLocation.Attribute("X").Value), double.Parse(fowLocation.Attribute("Y").Value));

            XElement fowSize = xDocument.Root.Element("FileOpenWindow").Element("Size");
            if (fowSize != null)
                fow_size = new Size(double.Parse(fowSize.Attribute("Width").Value), double.Parse(fowSize.Attribute("Height").Value));
        }

        private static XDocument buildSettingsDocument()
        {
            XDocument xDoc = new XDocument(new XElement("Settings"));

            // FileOpenWindow
            XElement xFOW = new XElement("FileOpenWindow");
            xDoc.Root.Add(xFOW);

            // add the Location (if set)
            if (fow_location.HasValue)
            {
                xFOW.Add(
                    new XElement("Location",
                       new XAttribute("X", fow_location.Value.X),
                       new XAttribute("Y", fow_location.Value.Y)
                    )
                );
            }
            
            // add the Location (if set)
            if (fow_size.HasValue)
            {
                xFOW.Add(
                    new XElement("Size",
                       new XAttribute("Width", fow_size.Value.Width),
                       new XAttribute("Height", fow_size.Value.Height)
                    )
                );
            }

            return xDoc;
        }
        #endregion
    }
}
